using UnityEngine;
using System.Collections;

public class lifePlayer : MonoBehaviour {
	
	public int life = 5;
	public float dead = 0;
	public float atack = 0;//velocidade de ataque
	public Collider colliderPlayer;
	public bool deadBool = false;
	public float tempo = 0;
	public GameObject Elpis;
	public int x;
	public GUIText text;
	public GameObject deadParticle;
	public Transform elpisTransform;
	public bool gameOver = false;
	
	// Use this for initialization
	void Start () {
	
		x = life;
		
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown (KeyCode.R)){
			Destroy(Elpis.gameObject);
			Application.LoadLevel(0);
		}
		
		if(life == 0){
			Debug.Log("morreu!");
			deadBool = true;
			text.text = "Fim de jogo, tecle 'R' pra reiniciar";
			gameOver = true;		
		
			
			}
		else{text.text = "Vida: "+life;}
		if (deadBool == true)
		{
			dead += Time.deltaTime;
		}
		
		//atack = atack + 0.1f;
		//Debug.Log("tempo :"+atack);
		if(life != 0)
		{
		tempo += Time.deltaTime;
//		Debug.Log("life"+life);
		}
		
		if(life == 0 && deadBool == true && gameOver == true)
		{
			Object particle = Instantiate (deadParticle, elpisTransform.position, Quaternion.identity);
					Destroy(particle, 0.9f);
			Debug.Log("Elpis morreu");
			gameOver = false;
		 }
			if (life == 0 && dead >=0.8)
			{
				deadBool = false;
				Elpis.transform.position = new Vector3(0,0,0);
				dead = 0;
				Destroy(deadParticle);

			}
		x = life;
		
	}	
		
	
	void OnTriggerStay(Collider collider){
		
		//colliderPlayer = collider;
		
		if(collider.gameObject.tag == "attackCollider")
		{
			if(tempo >= 2)
			{
				if(life>0){
					Debug.Log("dano");
					life--;
					
			}
			tempo = 0;
			}
		}			
	}
}
